from utils.funasr_client_api import Funasr_websocket_recognizer

# 提取FunASR模型初始化相关逻辑为单独函数
def init_funasr_recognizer():
    return Funasr_websocket_recognizer(
        host="127.0.0.1", port="10095", is_ssl=True, mode="2pass", chunk_size="0,10,5"
    )

def funasr_speech_to_text(audio_bytes: bytes, rcg, config) -> str:
    text = ''
    chunk_num = (len(audio_bytes) - 1) // config.CHUNK + 1
    for i in range(chunk_num):
        beg = i * config.CHUNK
        data = audio_bytes[beg: beg + config.CHUNK]
        resp = rcg.feed_chunk(data, wait_time=0.02)
        if len(resp) > 0 and resp['mode'] == '2pass-offline':
            text = resp['text']
    return text